<?php

namespace app\award;

use app\models\City;
use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\OrderActive;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use app\util\Util;
use yii\helpers\ArrayHelper;

class Award
{
    protected $order;
    protected $user;

    protected $set;

    protected $total_pay;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $this->total_pay = $this->order->total_pay - $this->order->total_freight;

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        // $is_repeat = ProductArea::checkRepeat($product_id);
        // $is_general = ProductArea::checkGeneral($product_id);
        // $is_presale = ProductArea::checkPresale($product_id);

        // var_dump($is_presale);exit;

        if ($is_bao) {
            $this->order->area = 1;
            $this->order->save();


            $this->bao();
            $this->second();
            $this->area();
        }



    }

    public function area()
    {
        $type = 13;
        $name = '区域奖';

        $address = UserAddress::find()
            ->where(['id' => $this->order->user_address])
            ->one();

        // var_dump($this->order->user_address, $address);exit;

        $sub = 0;

        $amount = $this->order->total_pay - $this->order->total_freight;


        // $vip2 = Chain::getReferrerChain($this->user->id, [UserLevel::$keys['vip2']], true);
        $vip2 = User::find()
            ->where(['address_up' => $address->area_id])
            ->one();
        if ($vip2) {
            // $vip2 = $vip2[0];
            // $commission = $this->set->q2 * $this->order->box;
            $commission = $this->set->q2 * $amount / 100;
            $this->saveCommission($vip2->id, $commission, $type, $name);
        } else {
            // $sub += $this->set->q2 * $this->order->box;
            $sub += $this->set->q2 * $amount / 100;
        }

        // $vip3 = Chain::getReferrerChain($this->user->id, [UserLevel::$keys['vip3']], true);
        $vip3 = User::find()
            ->where(['address_up' => $address->city_id])
            ->one();
        if ($vip3) {
            // $vip3 = $vip3[0];
            // $commission = $this->set->q3 * $this->order->box;
            $commission = $this->set->q3 * $amount / 100;
            $this->saveCommission($vip3->id, $commission, $type, $name);
        } else {
            // $sub += $this->set->q3 * $this->order->box;
            $sub += $this->set->q3 * $amount / 100;
        }

        // $vip4 = Chain::getReferrerChain($this->user->id, [UserLevel::$keys['vip4']], true);
        $vip4 = User::find()
            ->where(['address_up' => $address->province_id])
            ->one();
        if ($vip4) {
            // $vip4 = $vip4[0];
            // $commission = $this->set->q4 * $this->order->box;
            $commission = $this->set->q4 * $amount / 100;
            $this->saveCommission($vip4->id, $commission, $type, $name);
        } else {
            // $sub += $this->set->q4 * $this->order->box;
            $sub += $this->set->q4 * $amount / 100;
        }

        $this->sub($sub, '-区域奖');
    }

    // 余下返到指定帐户
    public function sub($commission, $name = '')
    {
        $id = SetUserUp::getLastOne()->user;
        if ($id && $commission >= 0.01) {
            $this->saveCommission($id, $commission, 20, '沉淀佣金' . $name);
        }
    }

    public function second()
    {
        $type = 12;
        $name = '间推奖';

        $r = Chain::getReferrer($this->order->user_id, true);
        if ($r) {
            $r = Chain::getReferrer($r->id, true);
            if ($r && $r->level >= UserLevel::$keys['vip1']) {
                $amount = $this->order->total_pay - $this->order->total_freight;
                $commission = $this->set->s / 100 * $amount;
                $this->saveCommission($r->id, $commission, $type, $name);
            } else {
                $amount = $this->order->total_pay - $this->order->total_freight;
                $commission = $this->set->s / 100 * $amount;
                $this->sub($commission, '-间推奖');
            }
        }
    }

    public function bao()
    {
        $type = 11;
        $name = '推荐奖';


        $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;
        $has_vip6 = false;

        $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;
        $diff_vip6 = 0;

        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
            UserLevel::$keys['vip6'],
        ], true);

        $amount = $this->order->total_pay - $this->order->total_freight;

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
                // continue;
            }

            if (( !$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip1']) {
                $diff_vip1 = $amount * $this->set->r1 / 100;
                $this->saveCommission($user->id, $diff_vip1, $type, $name);
                $has_vip1 = true;
            }

            if (( !$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $amount * $this->set->r2 / 100;
                $diff_vip2 -= $diff_vip1;
                $this->saveCommission($user->id, $diff_vip2, $type, $name);
                $has_vip2 = true;
            }

            if (( !$has_vip3 && !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $amount * $this->set->r3 / 100;
                $diff_vip3 -= $diff_vip1 + $diff_vip2;
                $this->saveCommission($user->id, $diff_vip3, $type, $name);
                $has_vip3 = true;
            }

            if (( !$has_vip4 && !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $amount * $this->set->r4 / 100;
                $diff_vip4 -= $diff_vip1 + $diff_vip2 + $diff_vip3;
                $this->saveCommission($user->id, $diff_vip4, $type, $name);
                $has_vip4 = true;
            }

            if (( !$has_vip5 && !$has_vip6) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $amount * $this->set->r5 / 100;
                $diff_vip5 -= $diff_vip1 + $diff_vip2 + $diff_vip3 + $diff_vip4;
                $this->saveCommission($user->id, $diff_vip5, $type, $name);
                $has_vip5 = true;
            }

            if (( !$has_vip6) && $user->level == UserLevel::$keys['vip6']) {
                $diff_vip6 = $amount * $this->set->r6 / 100;
                $diff_vip6 -= $diff_vip1 + $diff_vip2 + $diff_vip3 + $diff_vip4 + $diff_vip5;
                $this->saveCommission($user->id, $diff_vip6, $type, $name);
                $has_vip6 = true;
                break;
            }
        }

        // 总计
        $sub = $amount * $this->set->r6 / 100;
        $sub -= $diff_vip6 + $diff_vip5 + $diff_vip4 + $diff_vip3 + $diff_vip2 + $diff_vip1;
        // var_dump($sub);exit;
        $this->sub($sub, '-推荐奖');
    }






    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {

            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->user->id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();


            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->balance += $balance;
                // $account->await_balance += $balance;
                $account->save();
            }
        }
    }

}